package com.yanqu.road.dao.impl.recharge;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.enums.eOrderStatus;
import com.yanqu.road.entity.recharge.ThirdPartyRechargeOrder;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ThirdPartyRechargeDaoImpl extends TempDao {

	public List<ThirdPartyRechargeOrder> getThirdPartyRechargeOrderList(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<ThirdPartyRechargeOrder> dataList = new ArrayList<>();
		String sql = "select * from t_u_thirdparty_recharge where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				dataList.add(initThirdPartyRechargeOrder(rs));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public boolean addThirdPartyRechargeOrder(ThirdPartyRechargeOrder data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_thirdparty_recharge(userId,platformOrderNo,amount,ingots,orderStatus,createTime,updateTime,mallId,productType," +
				"realIngots,extraIngots,subCoinType,subCoinRatio,extraCoinType,extraCoinRatio,sdkOrderNo,currency,realMoney,realCurrency,localMoney,localCurrency,ltrRate,newMoney,tax,productCoin,device," +
				"payChannel,activityId,couponMoney,mallMarkId,gid,appid,payMethod,packageId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getPlatformOrderNo());
			call.setLong(i++, data.getAmount());
			call.setInt(i++, data.getIngots());
			call.setInt(i++, data.getOrderStatus().getValue());
			call.setTimestamp(i++, new Timestamp(data.getCreateTime().getTime()));
			call.setTimestamp(i++, new Timestamp(data.getUpdateTime().getTime()));
			call.setInt(i++, data.getMallId());
			call.setString(i++, data.getProductType());
			call.setInt(i++, data.getRealIngots());
			call.setInt(i++, data.getExtraIngots());
			call.setInt(i++, data.getSubCoinType());
			call.setString(i++, data.getSubCoinRatio());
			call.setInt(i++, data.getExtraCoinType());
			call.setString(i++, data.getExtraCoinRatio());
			call.setString(i++, data.getSdkOrderNo());
			call.setString(i++, data.getCurrency());
			call.setString(i++, data.getRealMoney());
			call.setString(i++, data.getRealCurrency());
			call.setString(i++, data.getLocalMoney());
			call.setString(i++, data.getLocalCurrency());
			call.setString(i++, data.getLtrRate());
			call.setString(i++, data.getNewMoney());
			call.setString(i++, data.getTax());
			call.setLong(i++, data.getProductCoin());
			call.setInt(i++, data.getDevice());
			call.setString(i++, data.getPayChannel());
			call.setInt(i++, data.getActivityId());
			call.setString(i++, data.getCouponMoney());
			call.setInt(i++, data.getMallMarkId());
			call.setString(i++, data.getGid());
			call.setString(i++, data.getAppid());
			call.setString(i++, data.getPayMethod());
			call.setString(i++, data.getPackageId());
            return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public ThirdPartyRechargeOrder getThirdPartyRechargeOrder(String platformOrderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_thirdparty_recharge where platformOrderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, platformOrderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				ThirdPartyRechargeOrder data = initThirdPartyRechargeOrder(rs);
				return data;
			}
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public boolean updateThirdPartyRechargeOrder(ThirdPartyRechargeOrder data) {
		String sql = "UPDATE t_u_thirdparty_recharge SET orderStatus=?,updateTime=?,vipExp=? WHERE platformOrderNo=? and orderStatus!=2";
		getDb().setConnName(DbNameStrategy.getUserDb());
		Map<Integer, DbParameter> para = new HashMap<>();
		int i = 1;
		para.put(i++, new DbParameter(Types.INTEGER, data.getOrderStatus().getValue()));
		para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(data.getUpdateTime().getTime())));
		para.put(i++, new DbParameter(Types.BIGINT, data.getVipExp()));
		para.put(i++, new DbParameter(Types.NVARCHAR, data.getPlatformOrderNo()));

		return getDb().executeNoneQuery(sql, para) > -1;
	}

	public boolean updateThirdPartyRechargeRepeatBuy(ThirdPartyRechargeOrder data) {
		String sql = "UPDATE t_u_thirdparty_recharge SET isRepeatBuy=? WHERE platformOrderNo=?";
		getDb().setConnName(DbNameStrategy.getUserDb());
		Map<Integer, DbParameter> para = new HashMap<>();
		int i = 1;
		para.put(i++, new DbParameter(Types.BOOLEAN, data.isRepeatBuy()));
		para.put(i++, new DbParameter(Types.NVARCHAR, data.getPlatformOrderNo()));
		return getDb().executeNoneQuery(sql, para) > -1;
	}

	public boolean checkThirdPartyRechargeOrder(String platformOrderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_thirdparty_recharge where platformOrderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, platformOrderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return false;
	}

	private ThirdPartyRechargeOrder initThirdPartyRechargeOrder(ResultSet rs) throws SQLException {
		ThirdPartyRechargeOrder data = new ThirdPartyRechargeOrder();
		data.setUserId(rs.getLong("userId"));
		data.setPlatformOrderNo(rs.getString("platformOrderNo"));
		data.setAmount(rs.getLong("amount"));
		data.setIngots(rs.getInt("ingots"));
		data.setOrderStatus(eOrderStatus.forValue(rs.getInt("orderStatus")));
		data.setCreateTime(rs.getDate("createTime"));
		data.setUpdateTime(rs.getDate("updateTime"));
		data.setMallId(rs.getInt("mallId"));
		data.setProductType(rs.getString("productType"));
		data.setRealIngots(rs.getInt("realIngots"));
		data.setExtraIngots(rs.getInt("extraIngots"));
		data.setSubCoinType(rs.getInt("subCoinType"));
		data.setSubCoinRatio(rs.getString("subCoinRatio"));
		data.setExtraCoinType(rs.getInt("extraCoinType"));
		data.setExtraCoinRatio(rs.getString("extraCoinRatio"));
		data.setSdkOrderNo(rs.getString("sdkOrderNo"));
		data.setCurrency(rs.getString("currency"));
		data.setRealMoney(rs.getString("realMoney"));
		data.setRealCurrency(rs.getString("realCurrency"));
		data.setLocalMoney(rs.getString("localMoney"));
		data.setLocalCurrency(rs.getString("localCurrency"));
		data.setLtrRate(rs.getString("ltrRate"));
		data.setNewMoney(rs.getString("newMoney"));
		data.setTax(rs.getString("tax"));
		data.setProductCoin(rs.getLong("productCoin"));
		data.setDevice(rs.getInt("device"));
		data.setPayChannel(rs.getString("payChannel"));
		data.setActivityId(rs.getInt("activityId"));
		data.setCouponMoney(rs.getString("couponMoney"));
		data.setMallMarkId(rs.getInt("mallMarkId"));
		data.setRepeatBuy(rs.getBoolean("isRepeatBuy"));
		return data;
	}
}
